/*
 * Powered By [fdlm]
 * Web Site: http://www.fdlm.cn
 * Since 2014 - 2015
 */


package com.sjyy.configuration.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javacommon.base.BaseRestSpringController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javacommon.page.Page;
import cn.org.rapid_framework.web.scope.Flash;

import com.sjyy.common.util.PropertiesUtil;
import com.sjyy.configuration.model.Configuration;
import com.sjyy.configuration.service.ConfigurationManager;
import com.sjyy.configuration.vo.query.ConfigurationQuery;

/**
 * @author cty email:seal100x@163.com
 * @version 1.0
 * @since 1.0
 */

@Controller
@RequestMapping("/configuration")
public class ConfigurationController extends BaseRestSpringController<Configuration,java.lang.Integer>{
	//默认多列排序,example: username desc,createTime asc
	protected static final String DEFAULT_SORT_COLUMNS = null; 
	
	private final String LIST_ACTION = "redirect:/configuration";
	
	public static ConfigurationManager configurationManager;
	public void setConfigurationManager(ConfigurationManager manager) {
		this.configurationManager = manager;
	}
	
	@InitBinder  
	public void initBinder(WebDataBinder binder) {  
	        binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true));  
	}
	   
	/** 增加了@ModelAttribute的方法可以在本controller方法调用前执行,可以存放一些共享变量,如枚举值,或是一些初始化操作 */
	@ModelAttribute
	public void init(ModelMap model) {
	}
	
	public static List<Configuration> ConfigurationList = null;
	
	
	/** 列表 */
	@RequestMapping
	public String index(ModelMap model,ConfigurationQuery query,HttpServletRequest request,HttpServletResponse response) {
		Page page = this.configurationManager.findPage(query);
		
		model.addAllAttributes(toModelMap(page, query));
		return "/configuration/index";
	}
	
	/** 显示 */
	@RequestMapping(value="/{id}")
	public String show(ModelMap model,@PathVariable java.lang.Integer id) throws Exception {
		Configuration configuration = (Configuration)configurationManager.getById(id);
		model.addAttribute("configuration",configuration);
		return "/configuration/show";
	}

	/** 进入新增 */
	@RequestMapping(value="/new")
	public String _new(ModelMap model,Configuration configuration,HttpServletRequest request,HttpServletResponse response) throws Exception {
		model.addAttribute("configuration",configuration);
		return "/configuration/new";
	}
	
	/** 保存新增 */
	@RequestMapping(method=RequestMethod.POST)
	public String create(ModelMap model,@Valid Configuration configuration,BindingResult errors,HttpServletRequest request,HttpServletResponse response) throws Exception {
		if(errors.hasErrors()) {
			return  "/configuration/new";
		}
		
		configurationManager.save(configuration);
		Flash.current().success(CREATED_SUCCESS); 
		return LIST_ACTION;
	}
	
	/** 编辑 */
	@RequestMapping(value="/{id}/edit")
	public String edit(ModelMap model,@PathVariable java.lang.Integer id) throws Exception {
		Configuration configuration = (Configuration)configurationManager.getById(id);
		model.addAttribute("configuration",configuration);
		return "/configuration/edit";
	}
	
	/** 保存更新 */
	@RequestMapping(value="/{id}",method=RequestMethod.PUT)
	public String update(ModelMap model,@PathVariable java.lang.Integer id,@Valid Configuration configuration,BindingResult errors,HttpServletRequest request,HttpServletResponse response) throws Exception {
		if(errors.hasErrors()) {
			return "/configuration/edit";
		}
		
		configurationManager.update(configuration);
		Flash.current().success(UPDATE_SUCCESS);
		return LIST_ACTION;
	}
	
	/** 删除 */
	@RequestMapping(value="/{id}",method=RequestMethod.DELETE)
	public String delete(ModelMap model,@PathVariable java.lang.Integer id) {
		configurationManager.removeById(id);
		Flash.current().success(DELETE_SUCCESS);
		return LIST_ACTION;
	}

	/** 批量删除 */
	@RequestMapping(method=RequestMethod.DELETE)
	public String batchDelete(ModelMap model,@RequestParam("items") java.lang.Integer[] items) {
		for(int i = 0; i < items.length; i++) {
			configurationManager.removeById(items[i]);
		}
		Flash.current().success(DELETE_SUCCESS);
		return LIST_ACTION;
	}
	
	/******************************************************************************************************************************/
	/**
	 * 刷新查询到得配置文件数据
	 **/
	@RequestMapping("/fresh")
	public @ResponseBody String fresh() throws Exception {
		PropertiesUtil.ConfigurationList =  configurationManager.findAll();
		return "sucess";
	}
	
	public static void initconfiguration(){
		PropertiesUtil.ConfigurationList =  configurationManager.findAll();
	}
	
}

